﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrTKChonMau : DevComponents.DotNetBar.Office2007Form
    {
        public FrTKChonMau()
        {
            InitializeComponent();
        }
        private string child = " - ",MaSP1,MaChiTiet1,Serial1="0";
        private void combo_load()
        {
            Data dta = new Data();
            FormProcess Frp = new FormProcess();
            string[] arr = new string[] { "KV1", "KV2","KV3","KV4","KV5_DG","KV5_NK" };
            for (int i = 0; i < arr.Length; i++)
            {
                cb_Chot.Items.Add(arr[i].ToString());
            }
            string sql_load = @"select MaSP,TenSP from dbo.SanPham";
            Frp.cb1(cb_MaSP, sql_load, 1, 0);
            
        }

        private void FrTKChonMau_Load(object sender, EventArgs e)
        {
            combo_load();
        }

        private void cb_MaSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process prs = new Process();
            FormProcess Frp = new FormProcess();
            MaSP1=prs.substring_right(cb_MaSP.Text, child);
            cb_MaCT.Items.Clear();
            string sql_load = @"select MaChiTiet,TenChiTiet from dbo.ChiTietSP where MaSP='"+MaSP1+"'";
            Frp.cb1(cb_MaCT, sql_load, 1, 0);
        }

        private void txt_SLPhieu_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SLPhieu);
        }

        private void txt_SLTT_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SLTT);
        }

        private void txt_SLMau_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SLMau);
        }

        private void txt_SLKoDat_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SLKoDat);
        }

        private void txt_BeCanh_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_BeCanh);
        }

        private void txt_CuaRoulo_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CuaRoulo);
        }

        private void txt_LocXuoc_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_LocXuoc);
        }

        private void txt_NutTetDau_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_NutTetDau);
        }

        private void txt_MeDauNan_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_MeDauNan);
        }

        private void txt_Lem_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Lem);
        }

        private void txt_NanSanSui_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_NanSanSui);
        }

        private void txt_ChaNham_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ChaNham);
        }

        private void txt_TramTret_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_TramTret);
        }

        private void txt_DinhViSai_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DinhViSaiKT);
        }

        private void txt_DuongVat_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DuongVatBD);
        }

        private void txt_DuongGhep_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DuongGhepCT);
        }

        private void txt_GoKT_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_GoKT);
        }

        private void txt_Keo_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_KeoRapDinh);
        }

        private void txt_SietOc_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SietOc);
        }

        private void txt_DongNut_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DongNut);
        }

        private void txt_CanMop_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CanMop);
        }

        private void txt_KheNan_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_KheNan);
        }

        private void txt_ChayDau_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ChayDau);
        }

        private void txt_TrayDau_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_TrayDau);
        }

        private void txt_ThieuDau_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ThieuDau);
        }

        private void txt_CanDau_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CanDau);
        }

        private void txt_MauKoDeu_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_MauKoDeu);
        }
        /*private void save()
        {
            
            Data dta = new Data();
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            string CanDau, BeCanh, CanMop, ChaNham, ChayDau, CuaRoulo, DinhViSai, DongNut, DuongGhep, DuongVat, GoKT, Keo, KheNan, Lem, LocXuoc, MeDauNan, NanSanSui, NutTetDau, SietOc, ThieuDau, TramTret, TrayDau, MauKhongDeu,Ngay1;
            string DuongGhepCT, DuongGhepHo, MatKoDung, CongVan, DuongRuot, MucMoc, Giac, KeoGhep, ThieuQC, DoAm, MatSong, DinhViSaiKT, DinhViSaiMat, DuongVatBD, DuongVatBoCanh, KeoRapDinh, HoMong, NanSaiMat, NanKoDungTC, CaoTet, NanCaoThap, BaChan, Tan, SaiMau;
            float SLPhieu1, SLTTe1, SLMau1, SLKoDat1;
            try
            {
                Conn.Open();
                cmd.Connection = Conn;
                Process prs = new Process();
                MaSP1 = prs.substring_right(cb_MaSP.Text, child);
                MaChiTiet1 = prs.substring_right(cb_MaCT.Text, child);
                //MessageBox.Show(MaChiTiet1 + " " + MaSP1);
                if (MaSP1 == "" || MaChiTiet1 == "" || txt_SLTT.Text == "" || txt_SLMau.Text == "" | txt_SLKoDat.Text == "")
                    MessageBox.Show("Thông tin nhập chưa chính xác hoặc thiếu");
                else
                {
                    string[] temp = txt_date.Text.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    SLPhieu1 = float.Parse(txt_SLPhieu.Text);
                    SLTTe1 = float.Parse(txt_SLTT.Text);
                    SLMau1 = float.Parse(txt_SLMau.Text);
                    SLKoDat1 = float.Parse(txt_SLKoDat.Text);
                    string sql_insert = @"insert into dbo.TK_ChonMau(MaSP, MaCT, SLPhieu, SLTTe, SLMau, SLKoDat, Chot,Ngay) values(N'";
                    sql_insert += MaSP1 + "',N'" + MaChiTiet1 + "'," + SLPhieu1 + "," + SLTTe1 + "," + SLMau1 + "," + SLKoDat1 + ",N'" + cb_Chot.Text +"','"+Ngay1+ "')";
                    cmd.CommandText = sql_insert;
                    //MessageBox.Show(sql_insert);
                    cmd.ExecuteNonQuery();
                    
                    sql_insert = @"select max(Serial_CM) from dbo.TK_ChonMau";
                    cmd.CommandText = sql_insert;
                    Serial1 = cmd.ExecuteScalar().ToString();
                    if (txt_BeCanh.Text == "")
                        BeCanh = "0";
                    else
                        BeCanh = txt_BeCanh.Text;
                    if (txt_CuaRoulo.Text == "")
                        CuaRoulo = "0";
                    else
                        CuaRoulo = txt_CuaRoulo.Text;
                    if (txt_LocXuoc.Text == "")
                        LocXuoc = "0";
                    else
                        LocXuoc = txt_LocXuoc.Text;

                    if (txt_NutTetDau.Text == "")
                        NutTetDau = "0";
                    else
                        NutTetDau = txt_NutTetDau.Text;
                    if (txt_MeDauNan.Text == "")
                        MeDauNan = "0";
                    else
                        MeDauNan = txt_MeDauNan.Text;
                    if (txt_Lem.Text == "")
                        Lem = "0";
                    else
                        Lem = txt_Lem.Text;
                    if (txt_NanSanSui.Text == "")
                        NanSanSui = "0";
                    else
                        NanSanSui = txt_NanSanSui.Text;
                    if (txt_ChaNham.Text == "")
                        ChaNham = "0";
                    else
                        ChaNham = txt_ChaNham.Text;
                    if (txt_TramTret.Text == "")
                        TramTret = "0";
                    else
                        TramTret = txt_TramTret.Text;
                    if (txt_DinhViSaiKT.Text == "")
                        DinhViSai = "0";
                    else
                        DinhViSai = txt_DinhViSaiKT.Text;
                    if (txt_DuongVatBD.Text == "")
                        DuongVat = "0";
                    else
                        DuongVat = txt_DuongVatBD.Text;
                    if (txt_DuongGhepCT.Text == "")
                        DuongGhep = "0";
                    else
                        DuongGhep = txt_DuongGhepCT.Text;
                    if (txt_GoKT.Text == "")
                        GoKT = "0";
                    else
                        GoKT = txt_GoKT.Text;
                    if (txt_KeoRapDinh.Text == "")
                        Keo = "0";
                    else
                        Keo = txt_KeoRapDinh.Text;
                    if (txt_SietOc.Text == "")
                        SietOc = "0";
                    else
                        SietOc = txt_SietOc.Text;
                    if (txt_DongNut.Text == "")
                        DongNut = "0";
                    else
                        DongNut = txt_DongNut.Text;
                    if (txt_CanMop.Text == "")
                        CanMop = "0";
                    else
                        CanMop = txt_CanMop.Text;
                    if (txt_KheNan.Text == "")
                        KheNan = "0";
                    else
                        KheNan = txt_KheNan.Text;
                    if (txt_ChayDau.Text == "")
                        ChayDau = "0";
                    else
                        ChayDau = txt_ChayDau.Text;
                    if (txt_TrayDau.Text == "")
                        TrayDau = "0";
                    else
                        TrayDau = txt_TrayDau.Text;
                    if (txt_ThieuDau.Text == "")
                        ThieuDau = "0";
                    else
                        ThieuDau = txt_ThieuDau.Text;
                    if (txt_CanDau.Text == "")
                        CanDau = "0";
                    else
                        CanDau = txt_CanDau.Text;
                    if (txt_MauKoDeu.Text == "")
                        MauKhongDeu = "0";
                    else
                        MauKhongDeu = txt_MauKoDeu.Text;
                    //string test1=txt_Lem.ToString();
                    sql_insert = @"insert into dbo.TK_ChiTietLoi(Serial_CM, BeCanh, DuongCua_Canroulo, LocXuoc, Nut_TetDau, MeDauNan, Lem, DauNanSanSui, ChaNhamKoSach, TramTretKoDay, DinhViSai, 
                                    DuongVatKoDat, DuongGhep, GoKT, Keo, SietOcKoDat, NutNhuaKoSat, CanMop, KheNan, ChayDau, TrayDau, ThieuDau, CanDau, MauKoDeu) values(";
                    sql_insert += Serial1 + "," + BeCanh + "," + CuaRoulo + "," + LocXuoc + "," + NutTetDau + "," + MeDauNan + "," + Lem + "," + NanSanSui + "," +ChaNham+",";
                    sql_insert += TramTret + "," + DinhViSai + "," + DuongVat + "," + DuongGhep + "," + GoKT + "," + Keo + "," + SietOc + "," + DongNut + "," + CanMop + "," + KheNan + "," + ChayDau+",";
                    sql_insert += TrayDau + "," + ThieuDau + "," + CanDau + "," + MauKhongDeu + ")";
                    //MessageBox.Show(sql_insert);
                    //MessageBox.Show(sql_insert);
                    cmd.CommandText = sql_insert;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ Liệu được thêm hoàn tất");
                    prs.reset_txt(groupPanel1);
                    prs.reset_txt(groupPanel2);
                    //cb_Chot.Text = "";
                    cb_MaSP.Focus();
                }
                
            }
            catch (Exception ex)
            {
                try
                {
                    string delete_Serial = @"delete from dbo.TK_ChonMau where Serial_CM=" + Serial1;
                    cmd.CommandText = delete_Serial;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Chứng từ này sẽ không insert vào DataBase do lỗi nhập liệu \n" + ex.Message);
                    Conn.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                //MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }*/
        private void Tinh()
        {
            double temp = 0;
            try
            {
                foreach (Control ct in groupPanel2.Controls)
                {
                    if (ct is TextBox)
                    {
                        if (((TextBox)ct).Text != "")
                        //((TextBox)ct).Text = "";
                        {
                            temp += double.Parse(((TextBox)ct).Text);
                        }
                    }
                }
                lb_Tinh.Text = temp.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void save()
        {
            string BeCanh1, LocXuoc1, NutTetDau1, Lem1, TramTret1, DuongGhepCT1, DuongGhepHo1, MatKoDungQD1, CongVan1, Duongruot1, MucMoc1, GiacKoDat1, KeoGhepDu1, ThieuQC1, DuongCuaRoulo1, DoAm1, MatSong1, MeDauNan1, DauNanSanSui1, ChaNham1, DinhViKT1, DinhViSai1,GoKT1="0",DongDau1,Khac1,LemDau1,RangNut1;
            string DuongVatBoDau1, DuongVatBoCanh1, KeoRapDinh1, XietOcKoChat1, DongNutNhua1, CanMop1, Homong1, NanSaiMat1, NanKoDungTC1, CaoTet1, NanKoDeu1, NanCaoThap1, BaChan1, TanKoDat1, ChayDau1, TrayDau1, ThieuDau1, CanDau1, SaiMau1, MauKoDeu1;
            string Ngay1,NguoiLayMau1;
            Data dta = new Data();
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            //string CanDau, BeCanh, CanMop, ChaNham, ChayDau, CuaRoulo, DinhViSai, DongNut, DuongGhep, DuongVat, GoKT, Keo, KheNan, Lem, LocXuoc, MeDauNan, NanSanSui, NutTetDau, SietOc, ThieuDau, TramTret, TrayDau, MauKhongDeu, Ngay1;
            //string DuongGhepCT, DuongGhepHo, MatKoDung, CongVan, DuongRuot, MucMoc, Giac, KeoGhep, ThieuQC, DoAm, MatSong, DinhViSaiKT, DinhViSaiMat, DuongVatBD, DuongVatBoCanh, KeoRapDinh, HoMong, NanSaiMat, NanKoDungTC, CaoTet, NanCaoThap, BaChan, Tan, SaiMau;
            float SLPhieu1, SLTTe1, SLMau1, SLKoDat1;
            try
            {
                Conn.Open();
                cmd.Connection = Conn;
                Process prs = new Process();
                MaSP1 = prs.substring_right(cb_MaSP.Text, child);
                MaChiTiet1 = prs.substring_right(cb_MaCT.Text, child);
                //MessageBox.Show(MaChiTiet1 + " " + MaSP1);
                if (MaSP1 == "" || MaChiTiet1 == "" || txt_SLTT.Text == ""||txt_date.Text=="")
                    MessageBox.Show("Thông tin nhập chưa chính xác hoặc thiếu");
                else
                {
                    string[] temp = txt_date.Text.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    SLPhieu1 = float.Parse(txt_SLPhieu.Text);
                    SLTTe1 = float.Parse(txt_SLTT.Text);
                    if (txt_SLMau.Text != "")
                        SLMau1 = float.Parse(txt_SLMau.Text);
                    else
                        SLMau1 = 0;
                    if (txt_SLKoDat.Text != "")
                        SLKoDat1 = float.Parse(txt_SLKoDat.Text);
                    else
                        SLKoDat1 = 0;
                    NguoiLayMau1=txt_NguoiLayMau.Text;
                    string sql_insert = @"insert into dbo.TK_ChonMau(MaSP, MaCT, SLPhieu, SLTTe, SLMau, SLKoDat, Chot,Ngay,NguoiLayMau) values(N'";
                    sql_insert += MaSP1 + "',N'" + MaChiTiet1 + "'," + SLPhieu1 + "," + SLTTe1 + "," + SLMau1 + "," + SLKoDat1 + ",N'" + cb_Chot.Text + "','" + Ngay1 + "',N'"+NguoiLayMau1+"')";
                    cmd.CommandText = sql_insert;
                    //MessageBox.Show(sql_insert);
                    cmd.ExecuteNonQuery();
                    sql_insert = @"select max(Serial_CM) from dbo.TK_ChonMau";
                    cmd.CommandText = sql_insert;
                    Serial1 = cmd.ExecuteScalar().ToString();
                    if (txt_BeCanh.Text == "")
                        BeCanh1 = "0";
                    else
                        BeCanh1 = txt_BeCanh.Text;
                    if (txt_LocXuoc.Text == "")
                        LocXuoc1 = "0";
                    else
                        LocXuoc1 = txt_LocXuoc.Text;
                    if (txt_NutTetDau.Text == "")
                        NutTetDau1 = "0";
                    else
                        NutTetDau1 = txt_NutTetDau.Text;
                    if (txt_Lem.Text == "")
                        Lem1 = "0";
                    else
                        Lem1 = txt_Lem.Text;
                    if (txt_TramTret.Text == "")
                        TramTret1 = "0";
                    else
                        TramTret1 = txt_TramTret.Text;
                    if (txt_DuongGhepCT.Text == "")
                        DuongGhepCT1 = "0";
                    else
                        DuongGhepCT1 = txt_DuongGhepCT.Text;
                    if (txt_DuongGhepHo.Text == "")
                        DuongGhepHo1 = "0";
                    else
                        DuongGhepHo1 = txt_DuongGhepHo.Text;
                    if (txt_Mat.Text == "")
                        MatKoDungQD1 = "0";
                    else
                        MatKoDungQD1 = txt_Mat.Text;
                    if (txt_CongVan.Text == "")
                        CongVan1 = "0";
                    else
                        CongVan1 = txt_CongVan.Text;
                    if (txt_DuongRuot.Text == "")
                        Duongruot1 = "0";
                    else
                        Duongruot1 = txt_DuongRuot.Text;
                    if (txt_MucMoc.Text == "")
                        MucMoc1 = "0";
                    else
                        MucMoc1 = txt_MucMoc.Text;
                    if (txt_Giac.Text == "")
                        GiacKoDat1 = "0";
                    else
                        GiacKoDat1 = txt_Giac.Text;
                    if (txt_KeoGhepDu.Text == "")
                        KeoGhepDu1 = "0";
                    else
                        KeoGhepDu1 = txt_KeoGhepDu.Text;
                    if (txt_ThieuQC.Text == "")
                        ThieuQC1 = "0";
                    else
                        ThieuQC1 = txt_ThieuQC.Text;
                    if (txt_CuaRoulo.Text == "")
                        DuongCuaRoulo1 = "0";
                    else
                        DuongCuaRoulo1 = txt_CuaRoulo.Text;
                    if (txt_DoAm.Text == "")
                        DoAm1 = "0";
                    else
                        DoAm1 = txt_DoAm.Text;
                    if (txt_MatKoDat.Text == "")
                        MatSong1 = "0";
                    else
                        MatSong1 = txt_MatKoDat.Text;
                    if (txt_MeDauNan.Text == "")
                        MeDauNan1 = "0";
                    else
                        MeDauNan1 = txt_MeDauNan.Text;
                    if (txt_NanSanSui.Text == "")
                        DauNanSanSui1 = "0";
                    else
                        DauNanSanSui1 = txt_NanSanSui.Text;
                    if (txt_ChaNham.Text == "")
                        ChaNham1 = "0";
                    else
                        ChaNham1 = txt_ChaNham.Text;
                    if (txt_DinhViSaiKT.Text == "")
                        DinhViKT1 = "0";
                    else
                        DinhViKT1 = txt_DinhViSaiKT.Text;
                    if (txt_DVSaiMat.Text == "")
                        DinhViSai1 = "0";
                    else
                        DinhViSai1 = txt_DVSaiMat.Text;
                    if (txt_DuongVatBD.Text == "")
                        DuongVatBoDau1 = "0";
                    else
                        DuongVatBoDau1 = txt_DuongVatBD.Text;
                    if (txt_DVBC.Text == "")
                        DuongVatBoCanh1 = "0";
                    else
                        DuongVatBoCanh1 = txt_DVBC.Text;
                    if (txt_KeoRapDinh.Text == "")
                        KeoRapDinh1 = "0";
                    else
                        KeoRapDinh1 = txt_KeoRapDinh.Text;
                    if (txt_SietOc.Text == "")
                        XietOcKoChat1 = "0";
                    else
                        XietOcKoChat1 = txt_SietOc.Text;
                    if (txt_DongNut.Text == "")
                        DongNutNhua1 = "0";
                    else
                        DongNutNhua1 = txt_DongNut.Text;
                    if (txt_CanMop.Text == "")
                        CanMop1 = "0";
                    else
                        CanMop1 = txt_CanMop.Text;
                    if (txt_HoMong.Text == "")
                        Homong1 = "0";
                    else
                        Homong1 = txt_HoMong.Text;
                    if (txt_NanSaiMat.Text == "")
                        NanSaiMat1 = "0";
                    else
                        NanSaiMat1 = txt_NanSaiMat.Text;
                    if (txt_NanKoDungTC.Text == "")
                        NanKoDungTC1 = "0";
                    else
                        NanKoDungTC1 = txt_NanKoDungTC.Text;
                    if (txt_CaoTet.Text == "")
                        CaoTet1 = "0";
                    else
                        CaoTet1 = txt_CaoTet.Text;
                    if (txt_KheNan.Text == "")
                        NanKoDeu1 = "0";
                    else
                        NanKoDeu1 = txt_KheNan.Text;
                    if (txt_NanCaoThap.Text == "")
                        NanCaoThap1 = "0";
                    else
                        NanCaoThap1 = txt_NanCaoThap.Text;
                    if (txt_BaChan.Text == "")
                        BaChan1 = "0";
                    else
                        BaChan1 = txt_BaChan.Text;
                    if (txt_TanKoDat.Text == "")
                        TanKoDat1 = "0";
                    else
                        TanKoDat1 = txt_TanKoDat.Text;
                    if (txt_ChayDau.Text == "")
                        ChayDau1 = "0";
                    else
                        ChayDau1 = txt_ChayDau.Text;
                    if (txt_TrayDau.Text == "")
                        TrayDau1 = "0";
                    else
                        TrayDau1 = txt_TrayDau.Text;
                    if (txt_ThieuDau.Text == "")
                        ThieuDau1 = "0";
                    else
                        ThieuDau1 = txt_ThieuDau.Text;
                    if (txt_CanDau.Text == "")
                        CanDau1 = "0";
                    else
                        CanDau1 = txt_CanDau.Text;
                    if (txt_SaiMau.Text == "")
                        SaiMau1 = "0";
                    else
                        SaiMau1 = txt_SaiMau.Text;
                    if (txt_MauKoDeu.Text == "")
                        MauKoDeu1 = "0";
                    else
                        MauKoDeu1 = txt_MauKoDeu.Text;
                    if (txt_DongDau.Text == "")
                        DongDau1 = "0";
                    else
                        DongDau1 = txt_DongDau.Text;
                    if (txt_LemDau.Text == "")
                        LemDau1 = "0";
                    else
                        LemDau1 = txt_LemDau.Text;
                    if (txt_RangNut.Text == "")
                        RangNut1 = "0";
                    else
                        RangNut1 = txt_RangNut.Text;
                    if (txt_Khac.Text == "")
                        Khac1 = "0";
                    else
                        Khac1 = txt_Khac.Text;
                    sql_insert = @"INSERT INTO [dbo].[TK_ChiTietLoi]
                   ([Serial_CM]
                   ,[BeCanh]
                   ,[DuongCua_Canroulo]
                   ,[LocXuoc]
                   ,[Nut_TetDau]
                   ,[MeDauNan]
                   ,[Lem]
                   ,[DauNanSanSui]
                   ,[ChaNhamKoSach]
                   ,[TramTretKoDay]
                   ,[DinhViSaiKT]
                   ,[DuongVatKoDat]
                   ,[DuongGhep]
                   ,[GoKT]
                   ,[KeoRapDinh]
                   ,[SietOcKoDat]
                   ,[NutNhuaKoSat]
                   ,[CanMop]
                   ,[KheNan]
                   ,[ChayDau]
                   ,[TrayDau]
                   ,[ThieuDau]
                   ,[CanDau]
                   ,[MauKoDeu]
                   ,[DuongGhepHo]
                   ,[MatKoDung]
                   ,[CongVan]
                   ,[DuongRuot]
                   ,[MucMoc]
                   ,[Giac]
                   ,[KeoGhepDu]
                   ,[ThieuQC]
                   ,[DoAm]
                   ,[MatSongKoDat]
                   ,[DinhViSaiMat]
                   ,[DuongVatBoCanh]
                   ,[HoMong]
                   ,[NanSaiMat]
                   ,[NanSaiTC]
                   ,[CaoTet]
                   ,[NanCaoThap]
                   ,[BaChan]
                   ,[TanKoDat]
                   ,[SaiMau]
                   ,[DongDau]
                   ,[LemDau]
                   ,[RangNut]
                   ,[Khac]) Values(";
                    sql_insert += Serial1 + "," + BeCanh1 + "," + DuongCuaRoulo1 + "," + LocXuoc1 + "," + NutTetDau1 + "," + MeDauNan1 + "," + Lem1 + "," + DauNanSanSui1 + "," + ChaNham1 + "," + TramTret1 + "," + DinhViKT1 + "," + DuongVatBoDau1 + "," + DuongGhepCT1 + "," + GoKT1 + "," + KeoRapDinh1+ ",";
                    sql_insert += XietOcKoChat1 + "," + DongNutNhua1 + "," + CanMop1 + "," + NanKoDeu1 + "," + ChayDau1 + "," + TrayDau1 + "," + ThieuDau1 + "," + CanDau1 + "," + MauKoDeu1 + "," + DuongGhepHo1 + "," + MatKoDungQD1 + "," + CongVan1 + "," + Duongruot1 + "," + MucMoc1 + "," + GiacKoDat1 + "," + KeoGhepDu1 + "," + ThieuQC1 + "," + DoAm1+ ",";
                    sql_insert += MatSong1 + "," + DinhViSai1 + "," + DuongVatBoCanh1 + "," + Homong1 + "," + NanSaiMat1 + "," + NanKoDungTC1 + "," + CaoTet1 + "," + NanCaoThap1 + "," + BaChan1 + "," + TanKoDat1 + "," + SaiMau1+","+DongDau1+","+LemDau1+","+RangNut1+","+Khac1+")";
                    //MessageBox.Show(sql_insert);
                    cmd.CommandText = sql_insert;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ Liệu được thêm hoàn tất");
                    //dataGridView1.Rows.Add(cb_MaSP.Text, cb_MaCT.Text, cb_Chot.Text, txt_date.Text, SLPhieu1, SLTTe1, SLMau1, SLKoDat1, NguoiLayMau1, BeCanh1, LocXuoc1, NutTetDau1, Lem1, TramTret1, DuongGhepCT1, DuongGhepHo1, MatKoDungQD1, CongVan1, Duongruot1, MucMoc1, GiacKoDat1,KeoGhepDu1, ThieuQC1, DuongCuaRoulo1, DoAm1, MatSong1, MeDauNan1, DauNanSanSui1, ChaNham1, DinhViKT1, DinhViSai1, DuongVatBoDau1, DuongVatBoCanh1, KeoRapDinh1, XietOcKoChat1, DongNutNhua1, CanMop1, Homong1, NanSaiMat1, NanKoDungTC1, CaoTet1, NanKoDeu1, NanCaoThap1, BaChan1, TanKoDat1, ChayDau1, TrayDau1, ThieuDau1, CanDau1, SaiMau1, MauKoDeu1,Serial1);
                    //prs.reset_txt(groupPanel1);
                    txt_SLMau.Text = "";
                    txt_SLKoDat.Text = "";
                    txt_SLPhieu.Text = "";
                    txt_SLTT.Text = "";
                    prs.reset_txt(groupPanel2);
                    //cb_Chot.Text = "";
                    cb_MaSP.Text = "";
                    cb_MaCT.Items.Clear();
                    cb_MaCT.Text = "";
                    cb_MaSP.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Đã có lỗi khi thêm dữ liệu \n"+ex.Message);
            }
        }
        private void Display(int i)
        {
            lb_serial.Text = dataGridView1.Rows[i].Cells["Serial_CM"].Value.ToString();
            cb_MaSP.Text = dataGridView1.Rows[i].Cells["MaSP"].Value.ToString();
            cb_MaCT.Text = dataGridView1.Rows[i].Cells["MaCT"].Value.ToString();
            cb_Chot.Text = dataGridView1.Rows[i].Cells["Chot"].Value.ToString();
            txt_date.Text = dataGridView1.Rows[i].Cells["Ngay"].Value.ToString();
            txt_SLPhieu.Text = dataGridView1.Rows[i].Cells["SLPhieu"].Value.ToString();
            txt_SLTT.Text = dataGridView1.Rows[i].Cells["SLTTe"].Value.ToString();
            txt_SLMau.Text = dataGridView1.Rows[i].Cells["SLMau"].Value.ToString();
            txt_SLKoDat.Text = dataGridView1.Rows[i].Cells["SLKoDat"].Value.ToString();
            txt_NguoiLayMau.Text = dataGridView1.Rows[i].Cells["NguoiLayMau"].Value.ToString();
            txt_BeCanh.Text = dataGridView1.Rows[i].Cells["BeCanh"].Value.ToString();
            txt_LocXuoc.Text = dataGridView1.Rows[i].Cells["LocXuoc"].Value.ToString();
            txt_NutTetDau.Text = dataGridView1.Rows[i].Cells["Nut_TetDau"].Value.ToString();
            txt_Lem.Text = dataGridView1.Rows[i].Cells["Lem"].Value.ToString();
            txt_TramTret.Text = dataGridView1.Rows[i].Cells["TramTretKoDay"].Value.ToString();
            txt_DuongGhepCT.Text = dataGridView1.Rows[i].Cells["DuongGhep"].Value.ToString();
            txt_DuongGhepHo.Text = dataGridView1.Rows[i].Cells["DuongGhepHo"].Value.ToString();
            txt_Mat.Text = dataGridView1.Rows[i].Cells["MatKoDung"].Value.ToString();
            txt_CongVan.Text = dataGridView1.Rows[i].Cells["CongVan"].Value.ToString();
            txt_DuongRuot.Text = dataGridView1.Rows[i].Cells["DuongRuot"].Value.ToString();
            txt_MucMoc.Text = dataGridView1.Rows[i].Cells["MucMoc"].Value.ToString();
            txt_Giac.Text = dataGridView1.Rows[i].Cells["Giac"].Value.ToString();
            txt_KeoGhepDu.Text = dataGridView1.Rows[i].Cells["KeoGhepDu"].Value.ToString();
            txt_ThieuQC.Text = dataGridView1.Rows[i].Cells["ThieuQC"].Value.ToString();
            txt_CuaRoulo.Text = dataGridView1.Rows[i].Cells["DuongCua_Canroulo"].Value.ToString();
            txt_DoAm.Text = dataGridView1.Rows[i].Cells["DoAm"].Value.ToString();
            txt_MatKoDat.Text = dataGridView1.Rows[i].Cells["MatSongKoDat"].Value.ToString();
            txt_MeDauNan.Text = dataGridView1.Rows[i].Cells["MeDauNan"].Value.ToString();
            txt_NanSanSui.Text = dataGridView1.Rows[i].Cells["DauNanSanSui"].Value.ToString();
            txt_ChaNham.Text = dataGridView1.Rows[i].Cells["ChaNhamKoSach"].Value.ToString();
            txt_DinhViSaiKT.Text = dataGridView1.Rows[i].Cells["DinhViSaiKT"].Value.ToString();
            txt_DVSaiMat.Text = dataGridView1.Rows[i].Cells["DinhViSaiMat"].Value.ToString();
            txt_DuongVatBD.Text = dataGridView1.Rows[i].Cells["DuongVatKoDat"].Value.ToString();
            txt_DVBC.Text = dataGridView1.Rows[i].Cells["DuongVatBoCanh"].Value.ToString();
            txt_KeoRapDinh.Text = dataGridView1.Rows[i].Cells["KeoRapDinh"].Value.ToString();
            txt_SietOc.Text = dataGridView1.Rows[i].Cells["SietOcKoDat"].Value.ToString();
            txt_DongNut.Text = dataGridView1.Rows[i].Cells["NutNhuaKoSat"].Value.ToString();
            txt_CanMop.Text = dataGridView1.Rows[i].Cells["CanMop"].Value.ToString();
            txt_HoMong.Text = dataGridView1.Rows[i].Cells["HoMong"].Value.ToString();
            txt_NanSaiMat.Text = dataGridView1.Rows[i].Cells["NanSaiMat"].Value.ToString();
            txt_NanKoDungTC.Text = dataGridView1.Rows[i].Cells["NanSaiTC"].Value.ToString();
            txt_CaoTet.Text = dataGridView1.Rows[i].Cells["CaoTet"].Value.ToString();
            txt_KheNan.Text = dataGridView1.Rows[i].Cells["KheNan"].Value.ToString();
            txt_NanCaoThap.Text = dataGridView1.Rows[i].Cells["NanCaoThap"].Value.ToString();
            txt_BaChan.Text = dataGridView1.Rows[i].Cells["BaChan"].Value.ToString();
            txt_TanKoDat.Text = dataGridView1.Rows[i].Cells["TanKoDat"].Value.ToString();
            txt_ChayDau.Text = dataGridView1.Rows[i].Cells["ChayDau"].Value.ToString();
            txt_TrayDau.Text = dataGridView1.Rows[i].Cells["TrayDau"].Value.ToString();
            txt_ThieuDau.Text = dataGridView1.Rows[i].Cells["ThieuDau"].Value.ToString();
            txt_CanDau.Text = dataGridView1.Rows[i].Cells["CanDau"].Value.ToString();
            txt_SaiMau.Text = dataGridView1.Rows[i].Cells["SaiMau"].Value.ToString();
            txt_MauKoDeu.Text=dataGridView1.Rows[i].Cells["MauKoDeu"].Value.ToString();
            txt_DongDau.Text = dataGridView1.Rows[i].Cells["DongDau"].Value.ToString();
            txt_LemDau.Text = dataGridView1.Rows[i].Cells["LemDau"].Value.ToString();
            txt_RangNut.Text = dataGridView1.Rows[i].Cells["RangNut"].Value.ToString();
            txt_Khac.Text = dataGridView1.Rows[i].Cells["Khac"].Value.ToString();

        }
        private void checkint(TextBox txt)
        {
            if (txt.Text == "")
                txt.Text = "0";
            else
                txt.Text = "100";
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            save();
        }

        private void FrTKChonMau_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyCode == Keys.F11)
                save();
            if (ex.KeyCode == Keys.F1)
            {
                Tinh();
            }

        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            Process prs = new Process();
            DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa Thông Tin có Số Serial " + lb_serial.Text, "Thông Báo", MessageBoxButtons.OKCancel);
            if (d == DialogResult.OK)
            {
                try
                {
                    string datecheck = @"select datediff(dd,Ngay,getdate()) from dbo.TK_ChonMau where Serial_CM='" + lb_serial.Text + "'";
                    string delete = @"delete from dbo.TK_ChonMau where Serial_CM='" + lb_serial.Text + "'";
                    Data dta = new Data();
                    SqlConnection Conn = dta.Connect();
                    Conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = Conn;
                    cmd.CommandText = datecheck;
                    int date_count = int.Parse(cmd.ExecuteScalar().ToString());
                    if (date_count <= 2)
                    {
                        cmd.CommandText = delete;
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Dữ liệu được xóa thành công");

                        lb_serial.Text = "0";
                    }
                    else
                    {
                        MessageBox.Show("Quá thời gian cho phép,Không được phép xóa");
                    }
                    Conn.Close();
                    cmd.Dispose();
                    prs.reset_txt(groupPanel2);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void txt_DuongGhepHo_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DuongGhepHo);
        }
        private void txt_HoMong_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_HoMong);
        }

        private void txt_Mat_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Mat);
        }

        private void txt_CongVan_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CongVan);
        }

        private void txt_DuongRuot_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DuongRuot);
        }

        private void txt_MucMoc_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_MucMoc);
        }

        private void txt_Giac_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Giac);
        }

        private void txt_KeoGhepDu_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_KeoGhepDu);
        }

        private void txt_ThieuQC_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ThieuQC);
        }

        private void txt_DoAm_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DoAm);
        }

        private void txt_MatKoDat_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_MatKoDat);
        }

        private void txt_DVSaiMat_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DVSaiMat);
        }

        private void txt_DVBC_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DVBC);
        }

        private void txt_NanSaiMat_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_NanSaiMat);
        }

        private void txt_NanKoDungTC_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_NanKoDungTC);
        }

        private void txt_CaoTet_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CaoTet);
        }

        private void txt_NanCaoThap_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_NanCaoThap);
        }

        private void txt_BaChan_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_BaChan);
        }

        private void txt_TanKoDat_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_TanKoDat);
        }

        private void txt_SaiMau_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SaiMau);
        }

        private void bt_prev_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_next_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i < dataGridView1.Rows.Count - 2)
                    {
                        i++;
                        Display(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_edit_Click(object sender, EventArgs e)
        {
            
            DialogResult d = MessageBox.Show("Bạn có chắc muốn sửa Thông Tin có số serial " + lb_serial.Text, "Thông Báo", MessageBoxButtons.OKCancel);
            if (d == DialogResult.OK)
            {
                try
                {
                    
                    //string sql_update = null;
                    string BeCanh1, LocXuoc1, NutTetDau1, Lem1, TramTret1, DuongGhepCT1, DuongGhepHo1, MatKoDungQD1, CongVan1, Duongruot1, MucMoc1, GiacKoDat1, KeoGhepDu1, ThieuQC1, DuongCuaRoulo1, DoAm1, MatSong1, MeDauNan1, DauNanSanSui1, ChaNham1, DinhViKT1, DinhViSai1,RangNut1,LemDau1;
                    string DuongVatBoDau1, DuongVatBoCanh1, KeoRapDinh1, XietOcKoChat1, DongNutNhua1, CanMop1, Homong1, NanSaiMat1, NanKoDungTC1, CaoTet1, NanKoDeu1, NanCaoThap1, BaChan1, TanKoDat1, ChayDau1, TrayDau1, ThieuDau1, CanDau1, SaiMau1, MauKoDeu1,DongDau1,Khac1;
                    string Ngay1, NguoiLayMau1;
                    Process prs = new Process();
                    SqlConnection Conn = prs.Connect();
                    SqlCommand cmd = new SqlCommand();
                    float SLPhieu1, SLTTe1, SLMau1, SLKoDat1;
                    //sql_update = @"update dbo.TKChonMau set ";
                    Conn.Open();
                    cmd.Connection = Conn;
                    MaSP1 = prs.substring_right(cb_MaSP.Text, child);
                    MaChiTiet1 = prs.substring_right(cb_MaCT.Text, child);
                    //MessageBox.Show(MaChiTiet1 + " " + MaSP1);
                    if (MaSP1 == "" || MaChiTiet1 == "" || txt_SLTT.Text == "" || txt_SLMau.Text == "" | txt_SLKoDat.Text == "")
                        MessageBox.Show("Thông tin nhập chưa chính xác hoặc thiếu");
                    else
                    {
                        string[] temp = txt_date.Text.Split('/');
                        Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                        SLPhieu1 = float.Parse(txt_SLPhieu.Text);
                        SLTTe1 = float.Parse(txt_SLTT.Text);
                        if (txt_SLMau.Text != "")
                            SLMau1 = float.Parse(txt_SLMau.Text);
                        else
                            SLMau1 = 0;
                        if (txt_SLKoDat.Text != "")
                            SLKoDat1 = float.Parse(txt_SLKoDat.Text);
                        else
                            SLKoDat1 = 0;
                        NguoiLayMau1 = txt_NguoiLayMau.Text;
                        string sql_update = @"update dbo.TK_ChonMau set MaSP=N'" + MaSP1 + "',MaCT=N'" + MaChiTiet1 + "',SLPhieu=" + SLPhieu1 + ",SLTTe=" + SLTTe1 + ",SLMau=" + SLMau1 + ",SLKoDat=" + SLKoDat1 + ",Chot=N'" + cb_Chot.Text + "',Ngay='" + Ngay1 + "',NguoiLayMau=N'" + NguoiLayMau1 + "' where Serial_CM="+lb_serial.Text;
                        //MessageBox.Show(sql_update);
                        cmd.CommandText = sql_update;
                        //MessageBox.Show(sql_insert);
                        cmd.ExecuteNonQuery();
                        if (txt_BeCanh.Text == "")
                            BeCanh1 = "0";
                        else
                            BeCanh1 = txt_BeCanh.Text;
                        if (txt_LocXuoc.Text == "")
                            LocXuoc1 = "0";
                        else
                            LocXuoc1 = txt_LocXuoc.Text;
                        if (txt_NutTetDau.Text == "")
                            NutTetDau1 = "0";
                        else
                            NutTetDau1 = txt_NutTetDau.Text;
                        if (txt_Lem.Text == "")
                            Lem1 = "0";
                        else
                            Lem1 = txt_Lem.Text;
                        if (txt_TramTret.Text == "")
                            TramTret1 = "0";
                        else
                            TramTret1 = txt_TramTret.Text;
                        if (txt_DuongGhepCT.Text == "")
                            DuongGhepCT1 = "0";
                        else
                            DuongGhepCT1 = txt_DuongGhepCT.Text;
                        if (txt_DuongGhepHo.Text == "")
                            DuongGhepHo1 = "0";
                        else
                            DuongGhepHo1 = txt_DuongGhepHo.Text;
                        if (txt_Mat.Text == "")
                            MatKoDungQD1 = "0";
                        else
                            MatKoDungQD1 = txt_Mat.Text;
                        if (txt_CongVan.Text == "")
                            CongVan1 = "0";
                        else
                            CongVan1 = txt_CongVan.Text;
                        if (txt_DuongRuot.Text == "")
                            Duongruot1 = "0";
                        else
                            Duongruot1 = txt_DuongRuot.Text;
                        if (txt_MucMoc.Text == "")
                            MucMoc1 = "0";
                        else
                            MucMoc1 = txt_MucMoc.Text;
                        if (txt_Giac.Text == "")
                            GiacKoDat1 = "0";
                        else
                            GiacKoDat1 = txt_Giac.Text;
                        if (txt_KeoGhepDu.Text == "")
                            KeoGhepDu1 = "0";
                        else
                            KeoGhepDu1 = txt_KeoGhepDu.Text;
                        if (txt_ThieuQC.Text == "")
                            ThieuQC1 = "0";
                        else
                            ThieuQC1 = txt_ThieuQC.Text;
                        if (txt_CuaRoulo.Text == "")
                            DuongCuaRoulo1 = "0";
                        else
                            DuongCuaRoulo1 = txt_CuaRoulo.Text;
                        if (txt_DoAm.Text == "")
                            DoAm1 = "0";
                        else
                            DoAm1 = txt_DoAm.Text;
                        if (txt_MatKoDat.Text == "")
                            MatSong1 = "0";
                        else
                            MatSong1 = txt_MatKoDat.Text;
                        if (txt_MeDauNan.Text == "")
                            MeDauNan1 = "0";
                        else
                            MeDauNan1 = txt_MeDauNan.Text;
                        if (txt_NanSanSui.Text == "")
                            DauNanSanSui1 = "0";
                        else
                            DauNanSanSui1 = txt_NanSanSui.Text;
                        if (txt_ChaNham.Text == "")
                            ChaNham1 = "0";
                        else
                            ChaNham1 = txt_ChaNham.Text;
                        if (txt_DinhViSaiKT.Text == "")
                            DinhViKT1 = "0";
                        else
                            DinhViKT1 = txt_DinhViSaiKT.Text;
                        if (txt_DVSaiMat.Text == "")
                            DinhViSai1 = "0";
                        else
                            DinhViSai1 = txt_DVSaiMat.Text;
                        if (txt_DuongVatBD.Text == "")
                            DuongVatBoDau1 = "0";
                        else
                            DuongVatBoDau1 = txt_DuongVatBD.Text;
                        if (txt_DVBC.Text == "")
                            DuongVatBoCanh1 = "0";
                        else
                            DuongVatBoCanh1 = txt_DVBC.Text;
                        if (txt_KeoRapDinh.Text == "")
                            KeoRapDinh1 = "0";
                        else
                            KeoRapDinh1 = txt_KeoRapDinh.Text;
                        if (txt_SietOc.Text == "")
                            XietOcKoChat1 = "0";
                        else
                            XietOcKoChat1 = txt_SietOc.Text;
                        if (txt_DongNut.Text == "")
                            DongNutNhua1 = "0";
                        else
                            DongNutNhua1 = txt_DongNut.Text;
                        if (txt_CanMop.Text == "")
                            CanMop1 = "0";
                        else
                            CanMop1 = txt_CanMop.Text;
                        if (txt_HoMong.Text == "")
                            Homong1 = "0";
                        else
                            Homong1 = txt_HoMong.Text;
                        if (txt_NanSaiMat.Text == "")
                            NanSaiMat1 = "0";
                        else
                            NanSaiMat1 = txt_NanSaiMat.Text;
                        if (txt_NanKoDungTC.Text == "")
                            NanKoDungTC1 = "0";
                        else
                            NanKoDungTC1 = txt_NanKoDungTC.Text;
                        if (txt_CaoTet.Text == "")
                            CaoTet1 = "0";
                        else
                            CaoTet1 = txt_CaoTet.Text;
                        if (txt_KheNan.Text == "")
                            NanKoDeu1 = "0";
                        else
                            NanKoDeu1 = txt_KheNan.Text;
                        if (txt_NanCaoThap.Text == "")
                            NanCaoThap1 = "0";
                        else
                            NanCaoThap1 = txt_NanCaoThap.Text;
                        if (txt_BaChan.Text == "")
                            BaChan1 = "0";
                        else
                            BaChan1 = txt_BaChan.Text;
                        if (txt_TanKoDat.Text == "")
                            TanKoDat1 = "0";
                        else
                            TanKoDat1 = txt_TanKoDat.Text;
                        if (txt_ChayDau.Text == "")
                            ChayDau1 = "0";
                        else
                            ChayDau1 = txt_ChayDau.Text;
                        if (txt_TrayDau.Text == "")
                            TrayDau1 = "0";
                        else
                            TrayDau1 = txt_TrayDau.Text;
                        if (txt_ThieuDau.Text == "")
                            ThieuDau1 = "0";
                        else
                            ThieuDau1 = txt_ThieuDau.Text;
                        if (txt_CanDau.Text == "")
                            CanDau1 = "0";
                        else
                            CanDau1 = txt_CanDau.Text;
                        if (txt_SaiMau.Text == "")
                            SaiMau1 = "0";
                        else
                            SaiMau1 = txt_SaiMau.Text;
                        if (txt_MauKoDeu.Text == "")
                            MauKoDeu1 = "0";
                        else
                            MauKoDeu1 = txt_MauKoDeu.Text;
                        if (txt_DongDau.Text == "")
                            DongDau1 = "0";
                        else
                            DongDau1 = txt_DongDau.Text;
                        if (txt_LemDau.Text == "")
                            LemDau1 = "0";
                        else
                            LemDau1 = txt_LemDau.Text;
                        if (txt_RangNut.Text == "")
                            RangNut1 = "0";
                        else
                            RangNut1 = txt_RangNut.Text;
                        if (txt_Khac.Text == "")
                            Khac1 = "0";
                        else
                            Khac1 = txt_Khac.Text;
                        sql_update = @"UPDATE [dbo].[TK_ChiTietLoi]
                                           SET [BeCanh] ="+BeCanh1+",[DuongCua_Canroulo] ="+DuongCuaRoulo1+",[LocXuoc] ="+LocXuoc1+",[Nut_TetDau] ="+NutTetDau1+",[MeDauNan] ="+MeDauNan1+",[Lem] ="+Lem1+",";
                        sql_update+=@"[DauNanSanSui] ="+DauNanSanSui1+",[ChaNhamKoSach] ="+ChaNham1+",[TramTretKoDay] ="+TramTret1+",[DinhViSaiKT] ="+DinhViKT1+",[DuongVatKoDat] ="+DuongVatBoDau1+",[DuongGhep] ="+DuongGhepCT1+",[KeoRapDinh] ="+KeoRapDinh1+",[SietOcKoDat] ="+XietOcKoChat1+",[NutNhuaKoSat] ="+DongNutNhua1+",[CanMop] ="+CanMop1+",[KheNan] ="+NanKoDeu1+",[ChayDau] ="+ChayDau1+",[TrayDau] ="+TrayDau1+",[ThieuDau]="+ThieuDau1+",[CanDau]="+CanDau1+",";
                        sql_update += @"[MauKoDeu]=" + MauKoDeu1 + ",[DuongGhepHo]=" + DuongGhepHo1 + ",[MatKoDung]=" + MatKoDungQD1 + ",[CongVan]=" + CongVan1 + ",[DuongRuot]=" + Duongruot1 + ",[MucMoc]=" + MucMoc1 + ",[Giac]=" + GiacKoDat1 + ",[KeoGhepDu]=" + KeoGhepDu1 + ",[ThieuQC]=" + ThieuQC1 + ",[DoAm]=" + DoAm1 + ",[MatSongKoDat]=" + MatSong1 + ",[DinhViSaiMat]=" + DinhViSai1 + ",[DuongVatBoCanh]=" + DuongVatBoCanh1 + ",[HoMong]=" + Homong1 + ",[NanSaiMat]=" + NanSaiMat1 + ",[NanSaiTC]=" + NanKoDungTC1 + ",[CaoTet]=" + CaoTet1 + ",[NanCaoThap]=" + NanCaoThap1 + ",[BaChan]=" + BaChan1 + ",[TanKoDat]=" + TanKoDat1 + ",[SaiMau]=" + SaiMau1 + ",DongDau="+DongDau1+",LemDau="+LemDau1+",RangNut="+RangNut1+",Khac="+Khac1+" where [Serial_CM]="+lb_serial.Text;
                        //MessageBox.Show(sql_update);
                        cmd.CommandText = sql_update;
                        cmd.ExecuteNonQuery();
                        //MessageBox.Show("Dữ Liệu được chỉnh sửa thành công");
                        //dataGridView1.Rows.Add(cb_MaSP.Text, cb_MaCT.Text, cb_Chot.Text, txt_date.Text, SLPhieu1, SLTTe1, SLMau1, SLKoDat1, NguoiLayMau1, BeCanh1, LocXuoc1, NutTetDau1, Lem1, TramTret1, DuongGhepCT1, DuongGhepHo1, MatKoDungQD1, CongVan1, Duongruot1, MucMoc1, GiacKoDat1, KeoGhepDu1, ThieuQC1, DuongCuaRoulo1, DoAm1, MatSong1, MeDauNan1, DauNanSanSui1, ChaNham1, DinhViKT1, DinhViSai1, DuongVatBoDau1, DuongVatBoCanh1, KeoRapDinh1, XietOcKoChat1, DongNutNhua1, CanMop1, Homong1, NanSaiMat1, NanKoDungTC1, CaoTet1, NanKoDeu1, NanCaoThap1, BaChan1, TanKoDat1, ChayDau1, TrayDau1, ThieuDau1, CanDau1, SaiMau1, MauKoDeu1, Serial1);
                        //prs.reset_txt(groupPanel1);
                        txt_SLMau.Text = "";
                        txt_SLKoDat.Text = "";
                        txt_SLPhieu.Text = "";
                        txt_SLTT.Text = "";
                        prs.reset_txt(groupPanel2);
                        bt_edit.Visible = false;
                        bt_prev.Visible = false;
                        bt_next.Visible = false;
                        bt_Search.Visible = false;
                        //cb_Chot.Text = "";
                        cb_MaSP.Focus();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Đã xảy ra lỗi khi chỉnh sửa, Chỉnh sửa không thành công\n" + ex.Message);
                }
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            bt_edit.Visible = true;
            bt_prev.Visible = true;
            bt_next.Visible = true;
            bt_Search.Visible = true;
        }

        private void bt_Search_Click(object sender, EventArgs e)
        {
            try
            {
                Process prs = new Process();
                MaSP1 = prs.substring_right(cb_MaSP.Text, child);
                MaChiTiet1 = prs.substring_right(cb_MaCT.Text, child);
                string[] temp = txt_date.Text.Split('/');
                string Ngay_search = temp[1] + "/" + temp[0] + "/" + temp[2];
                string search = @"SELECT     dbo.ChiTietSP.TenSP + ' - ' + dbo.TK_ChonMau.MaSP AS MaSP, dbo.ChiTietSP.TenChiTiet + ' - ' + dbo.TK_ChonMau.MaCT AS MaCT, 
                      dbo.TK_ChonMau.SLPhieu, dbo.TK_ChonMau.SLTTe, dbo.TK_ChonMau.SLMau, dbo.TK_ChonMau.SLKoDat, dbo.TK_ChonMau.NguoiLayMau, 
                      dbo.TK_ChonMau.Chot, CONVERT(char(10), dbo.TK_ChonMau.Ngay, 103) AS Ngay, dbo.TK_ChiTietLoi.BeCanh, dbo.TK_ChiTietLoi.DuongCua_Canroulo, 
                      dbo.TK_ChiTietLoi.LocXuoc, dbo.TK_ChiTietLoi.Nut_TetDau, dbo.TK_ChiTietLoi.MeDauNan, dbo.TK_ChiTietLoi.Lem, dbo.TK_ChiTietLoi.DauNanSanSui, 
                      dbo.TK_ChiTietLoi.ChaNhamKoSach, dbo.TK_ChiTietLoi.TramTretKoDay, dbo.TK_ChiTietLoi.DinhViSaiKT, dbo.TK_ChiTietLoi.DuongVatKoDat, 
                      dbo.TK_ChiTietLoi.DuongGhep, dbo.TK_ChiTietLoi.KeoRapDinh, dbo.TK_ChiTietLoi.SietOcKoDat, dbo.TK_ChiTietLoi.NutNhuaKoSat, 
                      dbo.TK_ChiTietLoi.CanMop, dbo.TK_ChiTietLoi.KheNan, dbo.TK_ChiTietLoi.ChayDau, dbo.TK_ChiTietLoi.TrayDau, dbo.TK_ChiTietLoi.ThieuDau, 
                      dbo.TK_ChiTietLoi.CanDau, dbo.TK_ChiTietLoi.MauKoDeu, dbo.TK_ChiTietLoi.DuongGhepHo, dbo.TK_ChiTietLoi.MatKoDung, 
                      dbo.TK_ChiTietLoi.CongVan, dbo.TK_ChiTietLoi.DuongRuot, dbo.TK_ChiTietLoi.MucMoc, dbo.TK_ChiTietLoi.Giac, dbo.TK_ChiTietLoi.KeoGhepDu, 
                      dbo.TK_ChiTietLoi.ThieuQC, dbo.TK_ChiTietLoi.DoAm, dbo.TK_ChiTietLoi.MatSongKoDat, dbo.TK_ChiTietLoi.DinhViSaiMat, 
                      dbo.TK_ChiTietLoi.DuongVatBoCanh, dbo.TK_ChiTietLoi.HoMong, dbo.TK_ChiTietLoi.NanSaiMat, dbo.TK_ChiTietLoi.NanSaiTC, 
                      dbo.TK_ChiTietLoi.CaoTet, dbo.TK_ChiTietLoi.NanCaoThap, dbo.TK_ChiTietLoi.BaChan, dbo.TK_ChiTietLoi.TanKoDat, dbo.TK_ChiTietLoi.SaiMau, dbo.TK_ChiTietLoi.DongDau, dbo.TK_ChiTietLoi.Khac,dbo.TK_ChiTietLoi.LemDau,dbo.TK_ChiTietLoi.RangNut,
                      dbo.TK_ChonMau.Serial_CM
                      FROM         dbo.TK_ChiTietLoi INNER JOIN
                      dbo.TK_ChonMau ON dbo.TK_ChiTietLoi.Serial_CM = dbo.TK_ChonMau.Serial_CM INNER JOIN
                      dbo.ChiTietSP ON dbo.TK_ChonMau.MaCT = dbo.ChiTietSP.MaChiTiet where dbo.TK_ChonMau.MaCT='" + MaChiTiet1 + "' and Chot='" + cb_Chot.Text + "' and Ngay='" + Ngay_search + "'";
                //MessageBox.Show(search);
                DataSet ds = prs.dts(search);
                dataGridView1.DataSource = ds.Tables[0];
                if (dataGridView1.Rows.Count > 1)
                {
                    int i = dataGridView1.Rows.Count - 2;
                    Display(i);
                }
                else
                    MessageBox.Show("Ko có dữ liệu");
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txt_DongDau_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_DongDau);
        }

        private void txt_Khac_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Khac);
        }

        private void txt_LemDau_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_LemDau);
        }

        private void txt_RangNut_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_LemDau);
        }
        

        




        

        
    }
}
